<?php
$CI = &get_instance();
?>

<form method="post">
	<input type="radio" name="thongke" value="theongay" <?php if($CI->input->post('thongke') == 'theongay') echo 'checked'; ?>/>Theo ngày
    <input type="radio" name="thongke" value="theothang" <?php if($CI->input->post('thongke') == 'theothang') echo 'checked'; ?>/>Theo tháng
    <input type="radio" name="thongke" value="theonam" <?php if($CI->input->post('thongke') == 'theonam') echo 'checked'; ?>/>Theo năm
    <br>
<?php
	echo form_date(array('name'=>'ngay'));
?>
	<br>
	Số lượng hiển thị: <input type="text" name="soluong" value="20" size="10"/>
    <br>
    <input type="submit" name="dongy" value="Đồng ý"/>
</form>

<?php
	if($CI->input->post('dongy'))
	{
		$time = get_form_date_value('ngay');
		$ngay = date('d', $time);
		$thang = date('m', $time);
		$nam = date('Y', $time);
		$soluonghienthi = $CI->input->post('soluong');
		
		if($CI->input->post('thongke') == 'theongay')
		{
			echo 'Ngày ' . $ngay . '/' . $thang . '/' . $nam . '<br>';
			$query = $CI->db->query("select taikhoan,hoten,count(tbl_thanhvien.id) soluong,sum(gia) tongtien from tbl_thanhvien, tbl_sim where tbl_thanhvien.id = tbl_sim.nguoimua and day(ngayban)=$ngay and month(ngayban)=$thang and year(ngayban)=$nam group by taikhoan order by sum(gia) desc, count(tbl_thanhvien.id) desc limit $soluonghienthi");
			$danhsachthanhvien = $query->result();
			$i = 0;
			foreach($danhsachthanhvien as $thanhvien)
			{
				$i++;
				echo $i . ': ' . $thanhvien->taikhoan . ' - ' . $thanhvien->hoten;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; số sim mua:';
				echo $thanhvien->soluong;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; tổng tiền:';
				echo $thanhvien->tongtien;
				echo '<br>';
			}			
		}
		else if($CI->input->post('thongke') == 'theothang')
		{
			echo 'Tháng ' . $thang . '/' . $nam . '<br>';
			$query = $CI->db->query("select taikhoan,hoten,count(tbl_thanhvien.id) soluong,sum(gia) tongtien from tbl_thanhvien, tbl_sim where tbl_thanhvien.id = tbl_sim.nguoimua and month(ngayban)=$thang and year(ngayban)=$nam group by taikhoan order by sum(gia) desc, count(tbl_thanhvien.id) desc limit $soluonghienthi");
			$danhsachthanhvien = $query->result();
			$i = 0;
			foreach($danhsachthanhvien as $thanhvien)
			{
				$i++;
				echo $i . ': ' . $thanhvien->taikhoan . ' - ' . $thanhvien->hoten;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; số sim mua:';
				echo $thanhvien->soluong;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; tổng tiền:';
				echo $thanhvien->tongtien;
				echo '<br>';
			}
		}
		else if($CI->input->post('thongke') == 'theonam')
		{
			echo 'Năm ' . $nam . '<br>';
			$query = $CI->db->query("select taikhoan,hoten,count(tbl_thanhvien.id) soluong,sum(gia) tongtien from tbl_thanhvien, tbl_sim where tbl_thanhvien.id = tbl_sim.nguoimua and year(ngayban)=$nam group by taikhoan order by sum(gia) desc, count(tbl_thanhvien.id) desc  limit $soluonghienthi");
			$danhsachthanhvien = $query->result();
			$i = 0;
			foreach($danhsachthanhvien as $thanhvien)
			{
				$i++;
				echo $i . ': ' . $thanhvien->taikhoan . ' - ' . $thanhvien->hoten;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; số sim mua:';
				echo $thanhvien->soluong;
				echo '<br>';
				echo '&nbsp;&nbsp;&nbsp; tổng tiền:';
				echo $thanhvien->tongtien;
				echo '<br>';
			}
		}
	}
?>